package com.luxsoft.sw3.kio.ui.consultas;

import java.util.List;

import javax.swing.Action;
import javax.swing.JPopupMenu;

import org.springframework.util.Assert;

import com.luxsoft.siipap.model.Periodo;
import com.luxsoft.siipap.model.core.Cliente;
import com.luxsoft.siipap.pos.ui.utils.FacturaUtils;
import com.luxsoft.siipap.swing.browser.FilteredBrowserPanel;
import com.luxsoft.sw3.cfd.model.ComprobanteFiscal;
import com.luxsoft.sw3.kio.ui.form.CorreoForm;
import com.luxsoft.sw3.services.Services;




public class CFDIPanel extends FilteredBrowserPanel<ComprobanteFiscal>{
	
	

	public CFDIPanel() {
		super(ComprobanteFiscal.class);
		setTitle("Comprobantes fiscales digitales (CFD)");		
	}
	
	public void init(){
		addProperty(
				"serie","tipo","folio","log.creado","receptor","rfc","total","estado","xmlPath"
				);
		addLabels(
				"Serie","Tipo","Folio","Fecha","Cliente","RFC","Total","Estado","XML"
				);
		installTextComponentMatcherEditor("Cliente", "receptor");
		installTextComponentMatcherEditor("RFC", "rfc");
		installTextComponentMatcherEditor("Serie", "serie");
		installTextComponentMatcherEditor("Folio", "folio");
		installTextComponentMatcherEditor("Tipo", "tipo");
		installTextComponentMatcherEditor("Total", "total");
		manejarPeriodo();
	}
	
	protected void manejarPeriodo(){
		periodo=Periodo.getPeriodo(-45);
	}
	
	@Override
	public Action[] getActions() {
		if(actions==null)
			actions=new Action[]{getLoadAction()
				//,getDeleteAction()
				//,getEditAction()
				,getViewAction()
				};
		return actions;
	}

	
	@Override
	protected void afterGridCreated() {		
		super.afterGridCreated();
		JPopupMenu popup=new JPopupMenu("Operaciones");
		for(Action a:getActions()){
			popup.add(a);
		}
		getGrid().setComponentPopupMenu(popup);
	}

	@Override
	protected List<ComprobanteFiscal> findData() {
		String hql="from ComprobanteFiscal c where date(c.log.creado) " +
				"between ? and ? ";
		return Services.getInstance().getHibernateTemplate().find(hql
				, new Object[]{periodo.getFechaInicial(),periodo.getFechaFinal()}
				);
	}
	
	@Override
	protected List<Action> createProccessActions(){
		List<Action> res=super.createProccessActions();
		res.add(addAction(null, "mandarPorCorreoElectronico", "Mandar por Correo"));
		return res;
	}

	@Override
	protected void doSelect(Object bean) {
		ComprobanteFiscal cfd=(ComprobanteFiscal)bean;
		if(cfd.getTipo().equals("FACTURA")){
			Long id=new Long(cfd.getOrigen());
			FacturaUtils.imprimirFactura(id, true);
		}
	}
	
	public void mandarPorCorreoElectronico(){
		if(!getSelected().isEmpty()){
			ComprobanteFiscal cfdi=(ComprobanteFiscal)getSelectedObject();
			Cliente cliente=Services.getInstance().getClientesManager().buscarPorRfc(cfdi.getRfc());
			Assert.notNull(cliente,"No localizo el cliente con RFC: "+cfdi.getRfc()+" En la base de datos");
			CorreoForm.mandarCorreo(cliente, getSelected());
		}
		
	}
	
	
	

}
